library(data.table)
library(ggplot2)
library(lfe)
library(stringr)
library(scales)
library(Hmisc)
library(stargazer)

hmda.in <- fread('../data/loans07to17conventional.csv') # Hmda data---conventional loans from 2007-2017 data
bd <- fread('../data/bankdata_conventional.csv')        # Bank capitalization data
with.avery <- merge(hmda.in,bd[,-c('bank','loan_amount','income'), with = F],by.x=c('rssdid','year'),by.y=c('rssdid','year'),all.x=T)

with.avery[,conf.pct := loan_amount / conforming_limit]
with.avery[,gse := as.integer(purchaser_type %in% 1:4)]
with.avery[,commercial := as.integer(purchaser_type %in% 6)]
with.avery[,other.purchaser := as.integer(purchaser_type %in% c(5,7,9))]
with.avery[,applicant_income := as.numeric(income)]
with.avery[,type := 'S']
with.avery[bank==1, type := 'B']
with.avery[,positive.crgap := as.integer(cr_gap>0)]
with.avery[,lender_state_year := paste(rssdid,state_code,year)]
with.avery[,lender_state := paste(rssdid,state_code)]


toReg <- with.avery

# Table 2 Panel A:
t2.a.1 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .01])
t2.a.2 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .05])
t2.a.3 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .10])
t2.a.4 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .25])
t2.a.5 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .01 & year == 2017])
t2.a.6 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .05 & year == 2017])
t2.a.7 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .10 & year == 2017])
t2.a.8 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .25 & year == 2017])

stargazer(t2.a.1,t2.a.2,t2.a.3,t2.a.4,t2.a.5,t2.a.6,t2.a.7,t2.a.8,type = 'html')


# Table 2 Panel B:
t2.b.1 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .01])
t2.b.2 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .05])
t2.b.3 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .10])
t2.b.4 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .25])
t2.b.5 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .01 & year == 2017])
t2.b.6 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .05 & year == 2017])
t2.b.7 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .10 & year == 2017])
t2.b.8 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .25 & year == 2017])

stargazer(t2.b.1,t2.b.2,t2.b.3,t2.b.4,t2.b.5,t2.b.6,t2.b.7,t2.b.8, type = 'html')

# Table 3 Panel A:
t3.a.1 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
t3.a.2 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
t3.a.3 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
t3.a.4 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
t3.a.5 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
t3.a.6 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
t3.a.7 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
t3.a.8 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(t3.a.1,t3.a.2,t3.a.3,t3.a.4,t3.a.5,t3.a.6,t3.a.7,t3.a.8, type = 'html')


# Table 3 Panel B:
t3.b.1 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
t3.b.2 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
t3.b.3 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
t3.b.4 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
t3.b.5 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
t3.b.6 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
t3.b.7 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
t3.b.8 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(t3.b.1,t3.b.2,t3.b.3,t3.b.4,t3.b.5,t3.b.6,t3.b.7,t3.b.8, type = 'html')


# Table 3 Panel B Appendix A1:
a1.1 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
a1.2 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
a1.3 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
a1.4 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id + year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
a1.5 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
a1.6 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
a1.7 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
a1.8 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | respondent_id,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(a1.1,a1.2,a1.3,a1.4,a1.5,a1.6,a1.7,a1.8, type = 'html')


## Rerun with lender-state-year level clusters
# Table 2 Panel A:
t2.a.1 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .01])
t2.a.2 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .05])
t2.a.3 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .10])
t2.a.4 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .25])
t2.a.5 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .01 & year == 2017])
t2.a.6 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .05 & year == 2017])
t2.a.7 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .10 & year == 2017])
t2.a.8 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .25 & year == 2017])

stargazer(t2.a.1,t2.a.2,t2.a.3,t2.a.4,t2.a.5,t2.a.6,t2.a.7,t2.a.8,type = 'html')

# Table 2 Panel B:
t2.b.1 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .01])
t2.b.2 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .05])
t2.b.3 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .10])
t2.b.4 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .25])
t2.b.5 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .01 & year == 2017])
t2.b.6 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .05 & year == 2017])
t2.b.7 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .10 & year == 2017])
t2.b.8 <- felm(bank ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .25 & year == 2017])

stargazer(t2.b.1,t2.b.2,t2.b.3,t2.b.4,t2.b.5,t2.b.6,t2.b.7,t2.b.8, type = 'html')

# Table 3 Panel A:
t3.a.1 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
t3.a.2 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
t3.a.3 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
t3.a.4 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
t3.a.5 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
t3.a.6 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
t3.a.7 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
t3.a.8 <- felm(held ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(t3.a.1,t3.a.2,t3.a.3,t3.a.4,t3.a.5,t3.a.6,t3.a.7,t3.a.8, type = 'html')

# Table 3 Panel B:
t3.b.1 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
t3.b.2 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
t3.b.3 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
t3.b.4 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
t3.b.5 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
t3.b.6 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
t3.b.7 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
t3.b.8 <- felm(positive.crgap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(t3.b.1,t3.b.2,t3.b.3,t3.b.4,t3.b.5,t3.b.6,t3.b.7,t3.b.8, type = 'html')

# Table 3 Panel B Appendix B1:
a1.1 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .01 & bank == 1])
a1.2 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .05 & bank == 1])
a1.3 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .10 & bank == 1])
a1.4 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state_year,data=toReg[abs(conf.pct-1) <= .25 & bank == 1])
a1.5 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .01 & year == 2017 & bank == 1])
a1.6 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .05 & year == 2017 & bank == 1])
a1.7 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .10 & year == 2017 & bank == 1])
a1.8 <- felm(cr_gap ~ jumbo + log(loan_amount) + log(applicant_income) | applicant_race_1 + applicant_sex + applicant_ethnicity + loan_purpose + property_type + paste(state_code,county_code,census_tract_number,year) | 0 | lender_state,data=toReg[abs(conf.pct-1) <= .25 & year == 2017 & bank == 1])

stargazer(a1.1,a1.2,a1.3,a1.4,a1.5,a1.6,a1.7,a1.8, type = 'html')

